<?php
namespace app\traits;
use app\BaseController;
use app\constants\HttpStatus;
use app\constants\ServiceStatus;
use think\App;
use think\facade\Request;

class ControllerTrait extends BaseController{
    use ResponseTrait;

    /**+
     * @var
     */
    protected $uid;
    public function __construct(App $app)
    {
        parent::__construct($app);
        //获取header中携带的token
        $token=$this->request->header("token");
        $this->uid=\app("jwt")->getUid($token);

        $token = Request::instance()->header('token');
        $res = checkToken($token);
        return $res;
    }

    /**
     * 重写验证规则
     * @param array $data
     * @param array|string $validate
     * @param array $message
     * @param bool $batch
     * @return array|bool|string|true
     */
    protected function validate (array $data, $validate, array $message = [], bool $batch = false){
        try {
            parent::validate($data, $validate, $message, $batch);
        } catch (\Exception $e) {
            $this->error(ServiceStatus::FAILED["CODE"],$e->getMessage(),HttpStatus::INVALID_REQUEST["CODE"]);
        }
        return true;
    }
}